using System.Collections.Generic;
using System.Linq;
using Godot;

namespace Arcane.Source.Client
{
    public static class Iter
    {
        public static T Pop<T>(ref IEnumerable<T> iter)
        {
            var ans = iter.First();
            iter = iter.Skip(1);
            return ans;
        }

        public static bool TryPop<T>(ref IEnumerable<T> iter, out T item)
        {
            try
            {
                item = iter.First();
                iter = iter.Skip(1);
                return true;
            }
            catch
            {
                item = default;
                return false;
            }
        }

        public static bool TryPop<[MustBeVariant] U>(ref IEnumerable<Variant> iter, out U item) 
        {
            if (TryPop(ref iter, out var i))
            {
                item = i.As<U>();
                return true;
            }
            item = default;
            return false;
        }

        public static bool TryTake<T>(this IEnumerable<T> iter, int count, out IEnumerable<T> sub)
        {
            try
            {
                sub = iter.Take(count);
                return true;
            }
            catch
            {
                sub = null;
                return false;
            }
        }
    }
}